<?php

namespace App\Tables;

use Illuminate\Database\Eloquent\SoftDeletes;

class Device extends BaseModel
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];

    protected $casts = [
        'ports' => 'json',
        'ladder_price' => 'json',
    ];

    protected $appends = [
        'status_text',
        'agent_name',
        'full_address',
        'qrcode_base64',
    ];

    /**
     * 自动生成设备编号
     * @return string
     */
    public function autoMakeDeviceSn()
    {
        return strtoupper(uniqid(date('ymdHi')));
    }

    /**
     * 设备状态文字
     * @return string
     */
    public function getStatusTextAttribute()
    {
        switch ($this->status) {
            case 'online':
                return '在线';
            case 'offline':
                return '离线';
            case 'fault':
                return '故障';
            default:
                $this->status;
        }
    }

    /**
     * 所属代理商姓名
     * @return mixed
     */
    public function getAgentNameAttribute()
    {
        return $this->agent->realname;
    }

    /**
     * 获取二维码base64格式
     * @return mixed
     */
    public function getQrcodeBase64Attribute()
    {
        return device_qrcode_base64($this->device_sn, 200);
    }

    /**
     * 获取省信息
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function province()
    {
        return $this->hasOne('App\Tables\Region', 'code', 'province_code');
    }

    /**
     * 获取城市信息
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function city()
    {
        return $this->hasOne('App\Tables\Region','code', 'city_code');
    }

    /**
     * 获取地区信息
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function district()
    {
        return $this->hasOne('App\Tables\Region', 'code', 'district_code');
    }

    /**
     * 获取完整的地址信息
     * @return string
     */
    public function getFullAddressAttribute()
    {
        $full_address = '';

        if (!empty($this->province)) {
            $full_address .= $this->province->name;
        }
        if (!empty($this->city)) {
            $full_address .= $this->city->name;
        }
        if (!empty($this->district)) {
            $full_address .= $this->district->name;
        }
        return $full_address . $this->attributes['address'];
    }

    /**
     * 设备所属代理商
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function agent()
    {
        return $this->hasOne('App\Tables\User', 'id', 'user_id');
    }

    /**
     * 获取所有下级代理的设备
     * @param $query
     * @param $id
     * @return mixed
     */
    public function scopeOfChildren($query, $id)
    {
        return $query->whereIn('user_id', function ($query) use ($id) {
            $query->select('ar.child_id as id')
                ->from('users as u')
                ->join('agent_relation as ar', 'u.id', '=', 'ar.parent_id')
                ->where(function ($query) use ($id) {
                    $query->orWhere('ar.parent_id', $id);
                    $query->orWhere('ar.child_id', $id);
                });
        });
    }
}
